package cn.crc.pjblog.pages;

import javax.servlet.http.HttpServletRequest;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.annotations.Persist;

import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.internal.TapestryInternalUtils;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.services.RequestGlobals;

import cn.crc.pjblog.data.AvailableSexs;

import cn.crc.model.BlogMember;
import cn.crc.model.dao.PublicDAO;
import cn.crc.pjblog.util.PublicMethod;
import cn.crc.spring.SpringBean;



public class Register {
	
	@Persist
	private String userName;
	
	private String password;
	private String confirmpassword;
	
	@Persist
	private String gender;
	@Persist
	private String email;
	@Persist
	private String homepage;
	@Persist
	private boolean hiddenEmail;
	
	private PublicMethod pm = PublicMethod.getInstance();
	
	private AvailableSexs gendergroup;
	
	
	@Inject 
	private  Messages message;
	
	@Inject
	@SpringBean("IPublicDAO")
	private PublicDAO  dao;
	
//	@Inject
//	private SuccessPage successPage;
	
	@Inject 
	private  RequestGlobals requestGlobals;
	
	@Component
	private  Form frmList;
	
		
//	private AvailableSexs sexs;
	private AvailableSexs sexsValue;	
	
	public AvailableSexs getSexsValue() {
		return sexsValue;
	}
	

	public void setSexsValue(AvailableSexs sexsValue) {
		this.sexsValue = sexsValue;
	}

	public AvailableSexs[] getSexs(){
		
		return AvailableSexs.values();
	}
	
//	void setup(){
//		if (this.sexs == null) 
//		
//	}	
	public String getLabel(){		
		return TapestryInternalUtils.getLabelForEnum(message,this.sexsValue);
	}
	public String getConfirmpassword() {
		return confirmpassword;
	}
	public String getEmail() {
		return email;
	}
	public String getGender() {
		return gender;
	}
	public boolean getHiddenEmail() {
		return hiddenEmail;
	}
	public String getHomepage() {
		return homepage;
	}
	public String getPassword() {
		return password;
	}
	public String getUserName() {
		return userName;
	}
	public void setConfirmpassword(String confirmpassword) {
		this.confirmpassword = confirmpassword;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public void setHiddenEmail(boolean hiddenEmail) {
		this.hiddenEmail = hiddenEmail;
	}
	public void setHomepage(String homepage) {
		this.homepage = homepage;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}


	public AvailableSexs getGendergroup() {
		return gendergroup;
	}
	
	public void setGendergroup(AvailableSexs fff){
		
		this.gendergroup = fff;
	}
	
	
	
	
	public PublicDAO getDao() {
		return dao;
	}


	public void setDao(PublicDAO dao) {
		this.dao = dao;
	}

	
	
	void pageLoaded(){
		
		this.setGendergroup(AvailableSexs.保密);
	}
	
	
	/**
	 * 提交注册用户的表单并提交到数据库
	 * */
	 void onActionFromFrmList(){
		HttpServletRequest  request = this.requestGlobals.getHTTPServletRequest();
		
		AvailableSexs sex;
		String memLastip = request.getRemoteAddr();
		
		String userName = this.getUserName();
		String password = this.getPassword();
		String confirmPassword  = this.getConfirmpassword();
		sex = this.getGendergroup(); 
		String email = this.getEmail();
		String homePage = this.getHomepage();
		boolean hiddenEmail = this.getHiddenEmail();
		
//		if (this.frmList.getHasErrors()){
			if ((pm.checkInputNull(password)) && (pm.checkInputNull(confirmPassword)))
			{
				System.out.println("都不为空");
				if (!password.equals(confirmPassword)){
					System.out.println("两次输入的密码不一致！");
					this.frmList.recordError("两次输入的密码不一致！");
				}
				else{
					System.out.println("两次输入的密码一致！");
				}
				
			}
			else{
				this.frmList.recordError("密码域或者确认密码域中有一个为空！");
				System.out.println("一个为空");
			}
//		}
//		if (this.frmList.getHasErrors())
//			return null;
		
		System.out.println("userName="+userName);
		System.out.println("password="+password);
		System.out.println("confirmPassword="+confirmPassword);
		System.out.println("sex="+sex.toString());
		System.out.println("email="+email);
		System.out.println("homePage="+homePage);
		System.out.println("hiddenEmail="+hiddenEmail);	
		System.out.println("memLastip="+memLastip);
		System.out.println("isValit=" + frmList.isValid());
		if (this.frmList.isValid())
		
		{
			BlogMember bm = new BlogMember();
			
			bm.setMemName(userName);
			bm.setMemPassword(password);
			bm.setMemSex(sex.toString());
			bm.setMemEmail(email);
			bm.setMemHomepage(homePage);
			bm.setMemHideemail(hiddenEmail);
			bm.setMemLastip(memLastip);
			
			try {
				this.getDao().saveOrUpdate(bm);
				System.out.println("保存完毕");
				
			} catch (Exception e) {
				// TODO: handle exception
				System.out.println(e.getMessage());
				this.frmList.recordError("用户注册失败，错误信息如下："+e.getMessage());
			}		
//			return "successPage";
		}
		else{
			System.out.println("错误发生了"+frmList.isValid());
		}
//		return null;
	}
//	public void onSuccess(){
//		this.frmList.recordError("用户注册成功!");
//		
//	}
	
}
